        <script type="text/javascript" charset="utf-8">
			$(document).ready(function() {
				$("#tgl_jual").datepicker({
					dateFormat  : "yy-mm-dd",        
					  changeMonth : true,
					  changeYear  : true					
				});
				$("#id_reject").change(function(){
					$.ajax({						
						url:'getRusak.php',
						dataType: "html",
						type:'POST',
						data:'id_reject='+$(this).val(),
						success: function(data) {
							var obj = $.parseJSON(data);						
							if(data=="[]"){
								$("#jumlah_rusak").val('');
										
							}else{							
								$("#jumlah_rusak").val(obj[0].jumlah_rusak);								
							}
						}
					})
				})
				
			} );
		</script>
<?php
if($_SESSION['bperbaikan']!=1){
	echo "Akses tidak diizinkan";
	exit();
}

$act = filter($_GET['act']);
$field = filter($_GET['field']);
$q = filter($_GET['q']);

if(empty($act)){	
?>		
					<!-- box / title -->
					<div class="title">
						<h5>Data Barang perbaikan</h5>								
						<div class="search global-nav">
						  <form method="GET" class="form-search js-search-form" action="index.php?pages=brg_perbaikan" id="global-nav-search" style="width:350px">
								<input type="hidden" name="pages" value="brg_perbaikan">
								<select name="field" id="birthday_month" aria-label="Month" class="" >
									<option value="" <?php echo ($field=="")?"selected":"";?>>Cari Berdasarkan:</option>
									<option value="tgl_jual" <?php echo ($field=="satuan")?"selected":"";?>>Satuan Rusak</option>
									<option value="nota" <?php echo ($field=="satuan_kembali")?"selected":"";?>>Satuan Kembali</option>
								</select>
				            	<label class="visuallyhidden" for="search-query">Search query</label>
				            	<input class="search-input" type="text" id="search-query" placeholder="Search" name="q" value="<?php echo $q;?>">
				            	<span class="search-icon js-search-action">
				              		<button type="submit" class="icon nav-search" tabindex="-1">
				              			<span class="visuallyhidden">Search</span>
				              		</button>
				            	</span>
				            	<input disabled="disabled" class="search-input search-hinting-input" type="text" id="search-query-hint" autocomplete="off" spellcheck="false">				              					              
							</form>
						</div>
					</div>
					<!-- end box / title -->
					<div class="table">										
						<table>
							<thead>
								<tr>
									<th class="selected"><input type="checkbox" class="checkall" /></th>
									<th class="action">No.</th>
									<th class="left">Nota Penjualan</th>
									<th>Jumlah </th>
                                	<th class="left">Satuan </th>
									<th class="action" colspan="2"><a href="?pages=brg_perbaikan&act=edit" class="icon-plus-sign" title="Tambah Data Baru"></a></th>									
								</tr>
							</thead>
							<tbody>
							<?php			

							$q2=$q;
							if($field=='status'){
								$q2 = (strtolower($q)=='aktif')?'1':'0';
							}
							
							$addQuery='';


							if(!empty($field))
								$addQuery .= " WHERE $field LIKE '%$q2%' ";

							$batas=10;
							$pg=(int) $_GET['pg'];
							if(empty($pg))
							{
								$posisi=0;
								$pg=1;
							}
							else
							{
								$posisi = ($pg-1) * $batas;
							}
							$sqlSup1 = mysql_query("SELECT * FROM tb_stock_reject $addQuery ORDER BY id_stock_reject DESC LIMIT $posisi, $batas");
							
							$no=$posisi+1;
							while($dataSup = mysql_fetch_array($sqlSup1)){								

								$status = ($dataSup['status']==1)? "Aktif":"Non Aktif";
							?>
								<tr>
									<td class="selected"><input type="checkbox" /></td>
									<td class="center"><?php echo $no;?></td>
                                     <?php 
										  $sql=mysql_query("SELECT nota,c.id_reject FROM tb_penjualan a LEFT JOIN tb_reject b ON a.id_penjualan=b.id_penjualan LEFT JOIN tb_stock_reject c ON b.id_reject=c.id_reject");
										  $sql2=mysql_fetch_array($sql);
									?>
									<td><?php echo $sql2['nota'];?></td>
									<td><?php echo $dataSup['jumlah'];?></td>
									<td><?php echo $dataSup['satuan'];?></td>
									<td class="action"><a href="?pages=brg_perbaikan&act=edit&id=<?php echo $dataSup['id_stock_reject'];?>" class="icon-edit" title="Ubah Data"></a></td>
									<td class="action">
										<a href="#" title="Hapus Data" onClick="if(confirm('Anda akan menghapus data ini?')) window.location='?pages=brg_perbaikan&act=del&id=<?php echo $dataSup['id_stock_reject'];?>';" class="icon-trash"></a>
									</td>								
								</tr>
							<?php	
								$no++;				
							}

							$sqlSup2 = mysql_query("SELECT * FROM tb_stock_reject $addQuery ORDER BY id_stock_reject DESC");
							$jumlahData = mysql_num_rows($sqlSup2);

							$file="?pages=brg_perbaikan&field=$field&q=$q";
							$jmlhalaman=ceil($jumlahData/$batas);
	
							?>															
							</tbody>
						</table>
						<!-- pagination -->
						<div class="pagination pagination-left">
							<div class="results">
								<span><?php echo "Showing Results ".($posisi+1)."-".($no-1)." of $jumlahData";?></span>
							</div>
							<ul class="pager">
								<?php
								//link ke halaman sebelumnya (previous)
								if($pg > 1){
									$previous=$pg-1;
									echo "<li><a href=$file&pg=1>&laquo; First</a></li>
									<li><a href=$file&pg=$previous>&laquo; Prev</a></li> ";
								}else{ 
									echo "<li class='disabled'>&laquo; First</li>
									<li class='disabled'>&laquo; Prev</li> ";
								}
								?>

								<?php
								//Link untuk Angka
								$angka=($pg > 3 ? " <li class='separator'>...</li> " : " ");
								for($i=$pg-2;$i<$pg;$i++)
								{
								  if ($i < 1) 
								      continue;
								  $angka .= "<li><a href='$file&pg=$i'>$i</a></li> ";
								}

								$angka .= " <li class='current'>$pg</li>";
								for($i=$pg+1;$i<($pg+3);$i++)
								{
								  if ($i > $jmlhalaman) 
								      break;
								  $angka .= "<li><a href='$file&pg=$i'>$i</a></li> ";
								}

								$angka .= ($pg+2<$jmlhalaman ? " <li class='separator'>...</li> <li><a href='$file&pg=$jmlhalaman'>$jmlhalaman</a></li>" : " ");

								echo "$angka";
								?>
								

								<?php
								//link kehalaman berikutnya (Next)
								if($pg < $jmlhalaman)
								{
									$next=$pg+1;
									echo "<li><a href='$file&pg=$next'>Next &raquo;</a></li>
									<li><a href='$file&pg=$jmlhalaman'>Last &raquo;</a></li>";
								}
								else
								{ 
									echo "<li class='disabled'>Next &raquo;</li>
									<li class='disabled'>Last &raquo;</li> ";
								}		
								?>													
							</ul>
					  </div>

						<!-- end pagination -->
						<!-- table action -->
						
						<!-- end table action -->	
						<?php
						$sqlRts = mysql_query("SELECT stock_perbaikan FROM tb_temp_rts LIMIT 1");
						$getRts = mysql_fetch_array($sqlRts);
						?>
						<p>&nbsp;</p>
						<p style="font:bold 14px Tahoma"><i>Total Stok Perbaikan : <?php echo $getRts['stock_perbaikan'];?> Karton</i></p>						
					</div>

<?php
}elseif ($act=='edit') {

	$id_stock_reject = (int) filter($_GET['id']);

	$sqlSup = mysql_query("SELECT * FROM tb_stock_reject WHERE id_stock_reject=$id_stock_reject");
	$dataSup = mysql_fetch_array($sqlSup);

	if(!empty($id_stock_reject)){
		?>
		<!-- box / title -->
		<div class="title">
			<h5>Ubah Data Barang perbaikan</h5>														
		</div>
		<!-- end box / title -->
		<?php
	}else{
		?>
		<!-- box / title -->
		<div class="title">
			<h5>Tambah Data Barang perbaikan</h5>														
		</div>
		<!-- end box / title -->
		<?php
	}

?>	

	<div class="table">
		<form method="POST" action="?pages=brg_perbaikan&act=update">
		<input type="hidden" name="id_stock_reject" value="<?php echo $dataSup['id_stock_reject'];?>">
		<table style="width:auto">
		   	<tbody>
		   		<tr>
					<td><label>Nota</label></td>
					<td>:</td>
					<td>
                    <select class="select" style="width: 210px;" name="id_reject" id="id_reject" placeholder="Nota" required>
                      <option >- PILIH Nota -</option>
                      
                      <?php
                            $select_jur = mysql_query("SELECT * FROM tb_reject a ,tb_penjualan b where a.id_penjualan=b.id_penjualan");
                            while($select_jur2 = mysql_fetch_array($select_jur)){
							 if($dataSup['id_penjualan']==$select_jur2['id_penjualan']){
						  	$selected = " selected ";
						  }else{
						  	$selected = "";
						  }
                      ?>
                      <option <?php echo $selected;?> value="<?php echo"$select_jur2[id_penjualan]";?>"><?php echo"$select_jur2[nota]";?></option>
                      <?php
                            }
                      ?>
                        </select>
			   	</tr>
                <?php 
                	$juml=mysql_query("SELECT * FROM tb_reject WHERE id_reject='$dataSup[id_reject]' ");
                	$juml2=mysql_fetch_array($juml);
                ?>
                <tr>
					<td><label>Jumlah Rusak</label></td>
					<td>:</td>
					<td><input type="text" size="30" name="jumlah_rusak" value="<?php echo $juml2['jumlah_rusak'];?>" id="jumlah_rusak" placeholder="jumlah rusak" readonly="true"></td>
			   	</tr>	
                <tr>
					<td><label>Jumlah Perbaikan<br />
				  </label></td>
					<td>:</td>
				 	<td><input type="text" value="<?php echo $dataSup['jumlah'];?>" size="30" name="jumlah" id="jumlah" placeholder="jumlah perbaikan" required/></td> 
			   	</tr>
                <tr>
					<td><label>Satuan</label></td>
					<td>:</td>
				 	<td><input type="text" value="Karton" size="30" name="satuan" id="satuan"  readonly="true" required/></td> 
			   	</tr>
			   	<tr>
			 		<td>&nbsp;</td>
			 		<td>&nbsp;</td>
			 		<td>
			 			<input type="hidden" value="" size="15" name="id">
						<input type="submit" value="Simpan" name="submit" class="btn btn-primary">
						<input type="Reset" value="Reset" class="btn">
			 		</td>
		   		</tr>
		 	</tbody>
		</table>
		</form>
	</div>
<?php
}elseif ($act=='update') {
	# code...
	$id_stock_reject = (int) $_POST['id_stock_reject'];
	$id_reject = $_POST['id_reject'];
	$jumlah_rusak=$_POST['jumlah_rusak'];
	$jumlah = $_POST['jumlah'];
	$satuan = $_POST['satuan'];

	$cek_reject = mysql_query("SELECT jumlah_rusak from tb_reject WHERE id_penjualan='$id_reject'");
	$cek_reject2 = mysql_fetch_array($cek_reject);
	$cek=$cek_reject2['jumlah_rusak'];

	$stock_perbaikan=mysql_query("SELECT stock_perbaikan from tb_temp_rts");
	$stock_perbaikan2=mysql_fetch_array($stock_perbaikan);
	$b=$stock_perbaikan2["stock_perbaikan"];

	$update_perbaikan=($b + $cek) - $jumlah;


	if(!empty($id_stock_reject)){	
	if($jumlah > $jumlah_rusak ){
				sukses('Data perbaikan anda melebihi jumlah rusak', "?pages=brg_perbaikan&act=edit&id=$id_reject");
			}
		else {
		$update = mysql_query("UPDATE tb_stock_reject SET id_reject='$id_reject',  jumlah='$jumlah', satuan='$satuan' WHERE id_stock_reject=$id_stock_reject");
		$update2=mysql_query("UPDATE tb_temp_rts SET stock_perbaikan='$update_perbaikan'");


		if($update && $update2)
			sukses('Data berhasil disimpan', '?pages=brg_perbaikan');
	}
}else{

		if($jumlah > $cek){
				sukses('Data perbaikan anda melebihi dari Jumlah stok barang rusak', '?pages=brg_perbaikan&act=edit');
			}
		else {
			# code...
			$insert = mysql_query("INSERT INTO tb_stock_reject VALUES(NULL, '$id_reject', '$jumlah', '$satuan')");

				$jum_temp = mysql_query("SELECT stock_perbaikan from tb_temp_rts");
				$jum_temp2 = mysql_fetch_array($jum_temp);
				$jum=$jum_temp2['stock_perbaikan'];

				$hasil=$jum + $jumlah;

				$update_temp=mysql_query("UPDATE tb_temp_rts SET stock_perbaikan='$hasil'");

				if($insert && $update_temp)
				sukses('Data berhasil disimpan', '?pages=brg_perbaikan');
			}
		}		
	
}elseif ($act=='del') {
	# code...
	$id_stock_reject = (int) filter($_GET['id']);

$stock_reject = mysql_query("SELECT * from tb_stock_reject where id_stock_reject='$id_stock_reject'");
$tes = mysql_fetch_array($stock_reject);
$jumlahnya = $tes['jumlah'];

$temp_perbaikan=mysql_query("SELECT stock_perbaikan from tb_temp_rts");
$temp_perbaikan2=mysql_fetch_array($temp_perbaikan);
$b3=$temp_perbaikan2["stock_perbaikan"];

if ($tes['id_stock_reject'] == true) {
	
	$total = $b3 - $jumlahnya;
	$sql_update_stok = mysql_query("UPDATE tb_temp_rts SET stock_perbaikan = $total");

}

	$delete = mysql_query("DELETE FROM tb_stock_reject WHERE id_stock_reject=$id_stock_reject");

	if($delete)
		sukses('Data berhasil dihapus', '?pages=brg_perbaikan');
}
?>					